package com.arlen.photo.photopickup.presenter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.media.upload.UploadListener;
import com.alibaba.sdk.android.media.upload.UploadOptions;
import com.alibaba.sdk.android.media.upload.UploadTask;
import com.alibaba.sdk.android.media.utils.FailReason;
import com.arlen.photo.photopickup.util.DensityUtils;
import com.arlen.photo.photopickup.util.FileSizeUtil;
import com.arlen.photo.photopickup.util.MediaUtils;
import com.wbao.dianniu.logical.DnApplication;
import com.wbao.dianniu.logical.GlobalContext;

import java.io.File;

/**
 * Created by Arlen on 2016/8/10 10:18.
 */
public class BasePhotoPresenter{

    private Activity mActivity;
    private IPhotoResult mPhotoResult;
    /**  网络图片处理service */
//	private MediaService mediaService;
	//namespace
	public static final String namespace = "dianniu";

    public BasePhotoPresenter(Activity activity) {
        this.mActivity = activity;
    }

    protected void setPhotoResult(IPhotoResult photoResult) {
        this.mPhotoResult = photoResult;
    }
    String mTaskId;
    protected void uploadImageUrl(final String type, final MediaUtils.ImageProperty imageProperty) {
        if (TextUtils.isEmpty(imageProperty.fullPath))
            return;
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... params) {
                File file = new File(imageProperty.fullPath);
                BitmapFactory.Options options = FileSizeUtil.getBitmapOptions(file.getPath());
                int screenMax = Math.max(DensityUtils.getWindowWidth(mActivity),
                        DensityUtils.getWindowHeight(mActivity));
                int imgMax = Math.max(options.outWidth, options.outHeight);
                int inSimpleSize = 1;
                if (screenMax <= imgMax) {
                    inSimpleSize = Math.max(screenMax, imgMax) / Math.min(screenMax, imgMax);
                }
                String compressPath = FileSizeUtil.compressBitmap(mActivity,
                        file.getAbsolutePath(),
                        Bitmap.CompressFormat.JPEG,
                        options.outWidth / inSimpleSize,
                        options.outHeight / inSimpleSize,
                        false,imageProperty.desc);
                
                final UploadOptions opts = new UploadOptions.Builder()
        		.tag(String.valueOf(SystemClock.elapsedRealtime()))
        		.dir("images")
        		.aliases(imageProperty.desc).build();
//                if(Build.VERSION.SDK_INT > 23){
                	if(null != DnApplication.wantuService){
                		mTaskId = DnApplication.wantuService.upload(new File(compressPath), opts, uploadListener, GlobalContext.getCdnToken());
                	}else{
                		Log.e("HeadPresenter", "无法获取多媒体服务！7");
                	}
                /*}else{
                	MediaService mediaService = AlibabaSDK.getService(MediaService.class);
                	if(null != mediaService){
                		mTaskId = mediaService.upload(new File(compressPath), namespace, opts, uploadListener);
                	}else{
                		Log.e("HeadPresenter", "无法获取多媒体服务！8");
                	}
                }*/
              return compressPath;
            }

            @Override
            protected void onPostExecute(final String fileName) {
                //TODO 用的retrofit 2.0上传图片一次上传一张
                if(true){
                    mPhotoResult.showUploadView("返回的url", imageProperty);
                }
            }
        }.execute();
    }
    
    /**
	 * 上传图片事件监听
	 */
	UploadListener uploadListener = new UploadListener() {
		
		@Override
		public void onUploading(UploadTask arg0) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onUploadFailed(UploadTask arg0, FailReason arg1) {
			// TODO Auto-generated method stub
			Log.e("BasePhotoPresenter", "upload failed");
		}
		
		@Override
		public void onUploadComplete(UploadTask arg0) {
			mTaskId=null;
			Log.e("BasePhotoPresenter", "upload Complete");
		}
		
		@Override
		public void onUploadCancelled(UploadTask arg0) {
			mTaskId=null;
		}
	};
    
}
